<?php
  class model {

  	protected $db;

     function __construct(&$db){
     $this->db=&$db;
    }

    function get_info($id="",$arg="",$sql="", $table="ns_tree",$arg_sql="id"){
      if ($sql !=""){
      $tmp=$this->db->result_array($this->db->query($sql));
         }else{
     	 		$tmp=$this->db->result_array($this->db->query("SELECT $arg From $table WHERE $arg_sql='$id' LIMIT 1;"));
         };

      if($arg !=""){
      		return $tmp [0] [$arg];

      	}else{

        return $tmp;
      	}



     }

	function save($id,$name,$show,$postArray,$idAlias){	if(isset($postArray['max_short_a']))$postArray['max_short_a']=1;
     	if(isset($postArray['short_a']))$postArray['short_a']=1;
	$this->db->query("START TRANSACTION;");
     $this->db->query("UPDATE ns_tree SET name='$name', show_me='$show',adress='".$postArray['adress']."',side='".$postArray['side']."',module='".$postArray['module']."',templates='".$postArray['templates']."',template_out=".$postArray['template_out'].",templates_int='".$postArray['templates_int']."',max_short_a='".$postArray['max_short_a']."',short_a='".$postArray['short_a']."' WHERE id='$id'");
      foreach ($postArray as $key=>$value){
     $this->db->query("UPDATE id_lang_text SET text='".$value."' WHERE lang='".$key."' AND id='$idAlias';");
      }
     $this->db->query("COMMIT;");
	}

	function save_d($id,$lang,$edit){
     $this->db->query("UPDATE id_lang_text SET  text='$edit'  WHERE id='$id' AND lang='$lang'");
	}

	function save_doc($id,$edit,$name,$show){     $this->db->query(" INSERT INTO ns_doc VALUES (0,'$edit',CURDATE(),'$id','$show','$name');");
	}





      function add_r($id,$name,$show_me=true,$adress="",$side="left"){
      }

    function del_cat($id){
     			$this->db->query("BEGIN;SELECT @treeLeft := lft, @treeRight := rgt, @treeWidth := rgt - lft + 1 FROM ns_tree WHERE id = '$id';DELETE FROM ns_tree WHERE lft BETWEEN @treeLeft AND @treeRight;UPDATE ns_tree SET rgt = rgt - @treeWidth WHERE rgt > @treeRight;UPDATE ns_tree SET lft = lft - @treeWidth WHERE lft > @treeRight;COMMIT;",true);

    }


	function registry($class,$name,$schedule,$standby,$side_schedule,$side_standby,$templates,$name_module,$help_an){     $this->db->query("INSERT INTO modules VALUES(0,'$class','$name','$schedule','$standby','$side_schedule','$side_standby','$templates','$name_module','$help_an');");

	}

	function create_table($xml,$name){	 $sql="CREATE TABLE $name (";     foreach($xml->module_database->field as $f){      $sql_t.=$f." ".$f['type']." ".$f['modifier']." , " ;

     }
     $sql.=$sql_t.")";
     $sql=str_replace(" , )"," )",$sql);
     $this->db->query($sql);
	}

	function return_side_module($arg){	 return $this->db->result_array($this->db->query("SELECT side From side_module WHERE id='$arg'"));

	}



	function return_task($arg,$id_template){	 if($arg=="events")
	  return $this->db->result_array($this->db->query("SELECT class From modules WHERE events=1 AND level_access <= ".$_SESSION['user']['level']." order by number_launch;"));
	   else	    return $this->db->result_array($this->db->query("SELECT sm.side,m.class,m.name From  modules AS m, side_module AS sm WHERE m.".$arg." ='1' AND sm.id=m.".$arg."Side AND sm.id_template='".$id_template."' AND m.level_access <=".$_SESSION['user']['level']." order by number_launch"));
	}

	function return_side($id=0){	 if($id==0){	  $tmp=$this->db->result_array($this->db->query("SELECT * From side_site WHERE id_template=( SELECT id From templates WHERE show_me='1')"));	  }else{      $tmp=$this->db->result_array($this->db->query("SELECT * From side_site WHERE id_template='$id'"));
      }
     return $tmp;
	}

	function return_config_mod(){	 $tmp=$this->db->result_array($this->db->query("SELECT * From modules"));
	 return $tmp  ;
	}








	function load_cat_modules($arg="id"){	 return $this->db->result_array($this->db->query("SELECT * From modules ORDER BY $arg;"));

	}

	function return_info_model3($arg){
	 return $this->db->row_array($this->db->query("SELECT * From modules WHERE id='$arg'ORDER BY id;"));
	}

	function return_info_model($arg){	 return $this->db->result_array($this->db->query("SELECT * From modules WHERE id='$arg'ORDER BY id;"));
	}
    function return_info_model2($arg){
	 return $this->db->query("SELECT * From modules WHERE id='$arg'ORDER BY id;");
	}
	function return_info_template(){	 return $this->db->result_array($this->db->query("SELECT * From templates  WHERE show_me=1;"));
	}

	function return_all_templates($opr=""){		if(empty($opr))	     return $this->db->result_array($this->db->query("SELECT * From templates;"));
	      else
	       return $this->db->result_array($this->db->query("SELECT * From templates WHERE show_me !='1';"));
	}

	function return_user(){     return $this->db->result_array($this->db->query("SELECT * From users as u, type_users as ut WHERE u.id=ut.id;"));
	}

	function return_element($arg){	 if($arg)
	   return $this->db->result_array($this->db->query("SELECT * From form_level WHERE id_level='$arg'"));
	  else
       return $this->db->result_array($this->db->query("SELECT * From form_level"));
	}

	function save_element($url,$level,$name,$id_level){	 $this->db->query("INSERT INTO form_level VALUES(0,'$url','$level','$name','$id_level')");
	}

	function return_level_form($url){	 return $this->db->result_array($this->db->query("SELECT level From form_level where url='$url'"));
	}

	function save_level($lv,$id){	 $this->db->query("UPDATE form_level SET level='$lv' WHERE id='$id';");
	}

	function return_user_type($id=""){	 if($id==""){	  return $this->db->result_array($this->db->query("SELECT * From type_users ;"));
	  }else{	 	return $this->db->result_array($this->db->query("SELECT * From type_users WHERE id='$id';"));
	  }
	}

	function save_user($name,$adress,$pass,$type,$name_p,$oper="insert"){	 if($oper=="insert")
	  $this->db->query("INSERT INTO users VALUES('$name','$adress','$pass',(SELECT id From type_users WHERE type='$type'));");
	   else
	    $this->db->query("UPDATE users SET name='$name', adress='$adress', pass='$pass', id=(SELECT id From type_users WHERE type='$type')  WHERE name='$name_p'");
	}

	function del_user($name){	 $this->db->query("DELETE FROM users WHERE name='$name';");
	}

	function save_login($login,$level,$id="insert"){     if($id=="insert"){	  $this->db->query("INSERT INTO type_users VALUES(0,'$login','$level');");
	  }else{	  	$this->db->query("UPDATE type_users SET type='$login',level='$level' WHERE id='$id' ;");
	  }
	}

	function del_login($id){	 $this->db->query("DELETE FROM type_users WHERE id='$id';");
	}




    function save_side_l_un($name,$arg,$id){
     $arg=!$arg;     $this->db->query("UPDATE side_site SET free='$arg' WHERE side='$name' and id_template='$id'");
    }

    function save_side($name,$id){     $this->db->query("INSERT INTO side_site VALUE('$name',1,'$id');");
    }

    function save_tmp_inf ($id,$id_n,$path_file,$name,$show_me ){
     $this->db->query("UPDATE templates SET show_me='0';
     UPDATE templates SET id='$id_n', name='$name',path_file='$path_file', show_me='$show_me'   WHERE id='$id';",true);
    }

    function tem_del_side($side,$id){     $this->db->query("DELETE FROM side_site WHERE side='$side' AND id_template='$id' ");
    }

    function registry_template($name,$name_project,$path_file,$path_small_img,$show_me){     $this->db->query("INSERT INTO templates VALUE(0,'$path_file','$name','$show_me','$path_small_img','$name_project')");
    }

    function del_templates($p_n){     $this->db->query("DELETE From templates WHERE name_project='$p_n'");
    }

    function query_pagination($pag,$sm,$oper,$arg,$lang,$search){     if(!empty($search))$sql_oper=" AND text LIKE'%".$search."%'";     if($oper=="doc_img_id")
      $sql_oper.=" ORDER BY id ";
       else
        $sql_oper.=" ORDER BY date ";

     if($arg=="up")
      $sql_oper.=" DESC  ";
       else
        $sql_oper.=" ASC ";     return	$this->db->result_array($this->db->query("SELECT SQL_CALC_FOUND_ROWS * From id_lang_text WHERE lang='$lang' ".$sql_oper."LIMIT $pag,$sm;"));
    }

    function get_doc($id,$lang){     $tmp=$this->db->result_array($this->db->query("SELECT text From id_lang_text WHERE id='$id' AND lang='$lang'"));
     return $tmp [0]['text'];
    }

    function save_mod_optoins($id,$class,$name,$ins,$help,$sch,$st,$tem,$even,$nl,$la,$arr){     $this->db->query("UPDATE modules SET id='$id', class='$class', name='$name',help_annotation='$help', schedule='$sch', standby='$st',  templates='$tem', name_module='$ins', events='$even', number_launch='$nl', level_access='$la' WHERE id='$id'");
     $this->db->query("DELETE FROM side_module WHERE id='".$class."_"."schedule"."' OR id='".$class."_"."standby"."'");
     for($i=1;$i<count($arr);$i++){     	if(is_numeric($arr[$i])){         $ii=$i;
         $id_template=$arr[$ii++];
     	 $id2=$class."_".$arr[$ii++];
     	 $side=$arr[$ii++];
         $this->db->query("INSERT INTO side_module VALUE('$id2','$side','$id_template');");

     	}
     }
    }

    function module_nl($id,$arg){
     if($arg=="up")
       $arg="+1";
        else
         $arg="-1";
       $this->db->query("BEGIN;
       SELECT @nl1 := number_launch,@nl3 := number_launch   FROM modules  WHERE id='$id';
       SELECT @nl2 := number_launch,@id2 :=id   FROM modules  WHERE number_launch=@nl1 $arg ;
       UPDATE modules SET number_launch=@nl2  WHERE id='$id';
       UPDATE modules SET number_launch=@nl3  WHERE id=@id2;
       COMMIT;",true);
    }

    function return_max_level(){     return $this->db->result_array($this->db->query("SELECT MAX(level) AS level FROM type_users"));
    }



    function GetALLModulesForPage($id){    return $this->db->result_array($this->db->query("SELECT modules.*,ns_doc.*,id_lang_text.*,doc_view.id as idg FROM modules,ns_doc,ns_tree,id_lang_text,doc_view WHERE   ns_doc.father_id='$id' AND ns_doc.module=modules.class AND ns_tree.id=ns_doc.father_id AND doc_view.id=ns_doc.id AND doc_view.text=id_lang_text.id ;"));
    }

    function GetModelFroCreate(){     return $this->db->result_array($this->db->query("SELECT * FROM modules WHERE s_create=1"));
    }

    function showHideDoc($id,$arg){    return $this->db->query("UPDATE ns_doc SET show_me=".$arg." WHERE id='$id'");
    }

   function saveSideDoc($id,$side){
    return $this->db->query("UPDATE ns_doc SET side_for_doc='".$side."' WHERE id=".$id."");
  	}

  	function GetAllAliasForLink($id){  	return $this->db->result_array($this->db->query("SELECT langDoc.* FROM ns_tree left join id_lang_text as langDoc ON ns_tree.alias=langDoc.id WHERE ns_tree.id='$id' ;"));
  	}

  	function GetInfoForLinkTree($id){  	return $this->db->row_array($this->db->query("SELECT * FROM ns_tree WHERE id='$id'"));
  	}

  	function returnLink($id){
	return $this->db->result_array($this->db->query("SELECT modules.*,ns_doc.* FROM ns_doc,ns_tree,modules WHERE ns_doc.father_id = ns_tree.id AND ns_tree.id=".$id." AND modules.class=ns_doc.module"));
	}

    function GetInfoForLinkTreeAndModule($id){
  	return $this->db->row_array($this->db->query("SELECT ns_tree.*,modules.* FROM ns_tree,modules WHERE modules.class=ns_tree.module AND ns_tree.name='".$id."'"));
  	}

	function returnFreeLink(){	return $this->db->result_array($this->db->query("SELECT ns_doc.* FROM ns_doc,ns_tree WHERE ns_doc.father_id != ns_tree.id"));
	}

	function add_doc($id_ns_tree,$id_ns_doc){    return $this->db->query("UPDATE ns_doc SET father_id='".$id_ns_tree."' WHERE id='".$id_ns_doc."'");
	}

  }
?>